/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.h5Data.service;

import com.fzhucc.modules.h5Data.domain.H5Data;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.h5Data.repository.H5DataRepository;
import com.fzhucc.modules.h5Data.domain.dto.H5DataDto;
import com.fzhucc.modules.h5Data.domain.dto.H5DataQueryCriteria;
import com.fzhucc.modules.h5Data.service.mapstruct.H5DataMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2024-05-22
 **/
@Service
@RequiredArgsConstructor
public class H5DataService {

    private final H5DataRepository h5DataRepository;
    private final H5DataMapper h5DataMapper;

    public Map<String, Object> queryAll(H5DataQueryCriteria criteria, Pageable pageable) {
        Page<H5Data> page = h5DataRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(h5DataMapper::toDto));
    }

    public List<H5DataDto> queryAll(H5DataQueryCriteria criteria) {
        return h5DataMapper.toDto(h5DataRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    public H5DataDto showH5Data() {
        Optional<H5Data> res = h5DataRepository.findById(1);
        if (res.isPresent()) {
            return h5DataMapper.toDto(res.get());
        } else {
            return null;
        }
    }

    @Transactional
    public H5DataDto findById(Integer id) {
        H5Data h5Data = h5DataRepository.findById(id).orElseGet(H5Data::new);
        ValidationUtil.isNull(h5Data.getId(), "H5Data", "id", id);
        return h5DataMapper.toDto(h5Data);
    }

    @Transactional(rollbackFor = Exception.class)
    public H5DataDto create(H5Data resources) {
        return h5DataMapper.toDto(h5DataRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(H5Data resources) {
        H5Data h5Data = h5DataRepository.findById(resources.getId()).orElseGet(H5Data::new);
        ValidationUtil.isNull(h5Data.getId(), "H5Data", "id", resources.getId());
        h5Data.copy(resources);
        h5DataRepository.save(h5Data);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            h5DataRepository.deleteById(id);
        }
    }

    public void download(List<H5DataDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (H5DataDto h5Data : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put(" s1", h5Data.getS1());
            map.put(" s2", h5Data.getS2());
            map.put(" s3", h5Data.getS3());
            map.put(" s4", h5Data.getS4());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}